﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MagicArenaFramework.GameObject.Components;
using BEPUphysics.Entities;
using Networking;
using Microsoft.Xna.Framework;
using BEPUphysics.Entities.Prefabs;
using MagicArenaFramework.GameObject.Components.ConcreteTypes;
using BEPUphysics;
using BEPUphysics.Collidables;
using MagicArenaXNA.Assets;
using MagicArenaXNA.Games;
using BEPUphysics.Collidables.MobileCollidables;
using MagicArenaFramework.Bepu;
using MagicArenaFramework.GameObject.Attributes;
using MagicArenaFramework.GameObject.Attributes.Concrete;

namespace MagicArenaXNA.GameObject.Spells.factory.objects
{
    class FireballOne : GoComposite
    {
        const float RADIUS = 0.5f;

        public FireballOne(Vector3 pos, Entity target, Space space, BepuGame game)
        {
            Entity entity = new Sphere(pos, RADIUS);
            entity.IsAffectedByGravity = false;
            EntityComponent entComp = new EntityComponent(entity, space);
            this.AddChild(entComp);


            EntityModelRender render = new EntityModelRender(entity, TestAssetHolder.Instance.Lucca, game.ModelDrawer);
            this.AddChild(render);

            EntityFollow follow = new EntityFollow(entity, target, nothing, onCollider, 50);
            this.AddChild(follow);
            
        }

        void explode()
        {
            this.Destroy();
        }

        void nothing(Collidable other)
        {

        }

        void onCollider(Collidable other)
        {
            dealDamage(other);
            explode();
        }

        private void dealDamage(Collidable other)
        {
            if (other is EntityCollidable)
            {
                Entity ent = (other as EntityCollidable).Entity;
                CustomTag tag = ent.Tag as CustomTag;
                if (tag != null)
                {
                    GoComposite comp = tag.Component as GoComposite;
                    GoComponent comp2;
                    if(comp !=null && comp.GetChild(typeof(ScalarAttributeHolder), out comp2)){
                        ScalarAttributeHolder att = comp2 as ScalarAttributeHolder;
                        float value;
                        if (att!=null && att.ValueFor(CurHealthAttribute.CurHealthFamily, out value))
                        {
                            // TODO update with spell damage amount
                            value -= 100;
                            att.AddUpdateValue(CurHealthAttribute.CurHealthFamily, value);
                        }
                    }
                }
            }
        }

        public override void RegisterSelf(GoComposite parent)
        {
            Parent = parent;
            base.RegisterSelf(parent);
        }
    }
}
